//
//  MovieVc.m
//  豆瓣
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "MovieVc.h"
#import "MovieTwoVC.h"
#import "MovieDetailVC.h"
#import "MovieTableViewCell.h"
#import "MovieListModel.h"

@interface MovieVc ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;  //数据

@end

@implementation MovieVc

NSString *movie_cell_id = @"movie_cell_id";





//解析数据
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        //电影列表接口文档:http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php
        
          _dataArray = [NSMutableArray array];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            //解析
            if (data != nil) {
                
                NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                
               
//                NSLog(@"%@",dataDic[@"result"]);
                for (NSDictionary *dic in dataDic[@"result"]) {
                    MovieListModel *model = [[MovieListModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [_dataArray addObject:model];
                }

                
                
            }
              
        }];
        
        
        
        
        [dataTask resume];
        
      
      
        
    }
    
    return self;
}






//视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:YES];
      [self.HUD hide:NO];      //小菊花停止
    [self.tableView reloadData];
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    
    
    [self setRightBarButtonItem]; //设置导航栏右按钮
    [self setTableView]; //设置tableView

    
}
#pragma mark 导航栏右按钮 及 事件
- (void)setRightBarButtonItem
{
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    MovieTwoVC *movieTwo = [[MovieTwoVC alloc] init];
    movieTwo.dataArrayTwo = self.dataArray;
    
    
    [self.navigationController pushViewController:movieTwo animated:YES];
    
}

#pragma mark tableView
- (void)setTableView
{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStyleGrouped];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:movie_cell_id];   //注册cell
}

#pragma mark TableView代理

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
  
}
//分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}


//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:movie_cell_id];
    
    
   

    cell.model = self.dataArray[indexPath.section];  //赋值
    
    return cell;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 100;
}


//cell 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MovieDetailVC *movieDetailVC = [[MovieDetailVC alloc] init];
    movieDetailVC.model = self.dataArray[indexPath.section]; //传值
    movieDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}




@end
